package org.apache.james.mime4j.descriptor;

import java.io.StringReader;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.field.datetime.DateTime;
import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
import org.apache.james.mime4j.field.datetime.parser.ParseException;
import org.apache.james.mime4j.field.language.ContentLanguageParser;
import org.apache.james.mime4j.field.mimeversion.MimeVersionParser;
import org.apache.james.mime4j.field.structured.StructuredFieldParser;
import org.apache.james.mime4j.util.MimeUtil;

public class MaximalBodyDescriptor extends DefaultBodyDescriptor
  implements RFC2045MimeDescriptor, RFC2183ContentDispositionDescriptor, RFC3066ContentLanguageDescriptor, RFC2557ContentLocationDescriptor, RFC1864ContentMD5Descriptor
{
  private static final int DEFAULT_MAJOR_VERSION = 1;
  private static final int DEFAULT_MINOR_VERSION;
  private String contentDescription;
  private DateTime contentDispositionCreationDate;
  private MimeException contentDispositionCreationDateParseException;
  private DateTime contentDispositionModificationDate;
  private MimeException contentDispositionModificationDateParseException;
  private Map contentDispositionParameters;
  private DateTime contentDispositionReadDate;
  private MimeException contentDispositionReadDateParseException;
  private long contentDispositionSize;
  private MimeException contentDispositionSizeParseException;
  private String contentDispositionType;
  private String contentId;
  private List contentLanguage;
  private MimeException contentLanguageParseException;
  private String contentLocation;
  private MimeException contentLocationParseException;
  private String contentMD5Raw;
  private boolean isContentDescriptionSet;
  private boolean isContentDispositionSet;
  private boolean isContentIdSet;
  private boolean isContentLanguageSet;
  private boolean isContentLocationSet;
  private boolean isContentMD5Set;
  private boolean isMimeVersionSet;
  private int mimeMajorVersion;
  private int mimeMinorVersion;
  private MimeException mimeVersionException;

  protected MaximalBodyDescriptor()
  {
    this(null);
  }

  public MaximalBodyDescriptor(BodyDescriptor paramBodyDescriptor)
  {
    super(paramBodyDescriptor);
    this.isMimeVersionSet = false;
    this.mimeMajorVersion = 1;
    this.mimeMinorVersion = 0;
    this.contentId = null;
    this.isContentIdSet = false;
    this.contentDescription = null;
    this.isContentDescriptionSet = false;
    this.contentDispositionType = null;
    this.contentDispositionParameters = Collections.EMPTY_MAP;
    this.contentDispositionModificationDate = null;
    this.contentDispositionModificationDateParseException = null;
    this.contentDispositionCreationDate = null;
    this.contentDispositionCreationDateParseException = null;
    this.contentDispositionReadDate = null;
    this.contentDispositionReadDateParseException = null;
    this.contentDispositionSize = -1L;
    this.contentDispositionSizeParseException = null;
    this.isContentDispositionSet = false;
    this.contentLanguage = null;
    this.contentLanguageParseException = null;
    this.isContentIdSet = false;
    this.contentLocation = null;
    this.contentLocationParseException = null;
    this.isContentLocationSet = false;
    this.contentMD5Raw = null;
    this.isContentMD5Set = false;
  }

  private void parseContentDescription(String paramString)
  {
    if (paramString == null)
      this.contentDescription = "";
    while (true)
    {
      this.isContentDescriptionSet = true;
      return;
      this.contentDescription = paramString.trim();
    }
  }

  private void parseContentDisposition(String paramString)
  {
    String str2;
    this.isContentDispositionSet = true;
    this.contentDispositionParameters = MimeUtil.getHeaderParams(paramString);
    this.contentDispositionType = ((String)this.contentDispositionParameters.get(""));
    String str1 = (String)this.contentDispositionParameters.get("modification-date");
    if (str1 != null);
    try
    {
      this.contentDispositionModificationDate = parseDate(str1);
      str2 = (String)this.contentDispositionParameters.get("creation-date");
      if (str2 != null);
    }
    catch (ParseException localParseException1)
    {
      String str3;
      try
      {
        this.contentDispositionCreationDate = parseDate(str2);
        str3 = (String)this.contentDispositionParameters.get("read-date");
        if (str3 != null);
      }
      catch (ParseException localParseException1)
      {
        String str4;
        try
        {
          this.contentDispositionReadDate = parseDate(str3);
          str4 = (String)this.contentDispositionParameters.get("size");
          if (str4 != null);
        }
        catch (ParseException localParseException1)
        {
          try
          {
            while (true)
            {
              while (true)
              {
                this.contentDispositionSize = Long.parseLong(str4);
                this.contentDispositionParameters.remove("");
                return;
                localParseException3 = localParseException3;
                this.contentDispositionModificationDateParseException = localParseException3;
              }
              localParseException2 = localParseException2;
              this.contentDispositionCreationDateParseException = localParseException2;
            }
            localParseException1 = localParseException1;
            this.contentDispositionReadDateParseException = localParseException1;
          }
          catch (NumberFormatException localNumberFormatException)
          {
            while (true)
              this.contentDispositionSizeParseException = ((MimeException)new MimeException(localNumberFormatException.getMessage(), localNumberFormatException).fillInStackTrace());
          }
        }
      }
    }
  }

  private void parseContentId(String paramString)
  {
    if (paramString == null)
      this.contentId = "";
    while (true)
    {
      this.isContentIdSet = true;
      return;
      this.contentId = paramString.trim();
    }
  }

  private DateTime parseDate(String paramString)
    throws ParseException
  {
    return new DateTimeParser(new StringReader(paramString)).date_time();
  }

  private void parseLanguage(String paramString)
  {
    this.isContentLanguageSet = true;
    if (paramString != null);
    try
    {
      this.contentLanguage = new ContentLanguageParser(new StringReader(paramString)).parse();
      return;
    }
    catch (MimeException localMimeException)
    {
      this.contentLanguageParseException = localMimeException;
    }
  }

  private void parseLocation(String paramString)
  {
    StructuredFieldParser localStructuredFieldParser;
    this.isContentLocationSet = true;
    if (paramString != null)
    {
      localStructuredFieldParser = new StructuredFieldParser(new StringReader(paramString));
      localStructuredFieldParser.setFoldingPreserved(false);
    }
    try
    {
      this.contentLocation = localStructuredFieldParser.parse();
      return;
    }
    catch (MimeException localMimeException)
    {
      this.contentLocationParseException = localMimeException;
    }
  }

  private void parseMD5(String paramString)
  {
    this.isContentMD5Set = true;
    if (paramString != null)
      this.contentMD5Raw = paramString.trim();
  }

  private void parseMimeVersion(String paramString)
  {
    MimeVersionParser localMimeVersionParser = new MimeVersionParser(new StringReader(paramString));
    try
    {
      localMimeVersionParser.parse();
      int i = localMimeVersionParser.getMajorVersion();
      if (i != -1)
        this.mimeMajorVersion = i;
      int j = localMimeVersionParser.getMinorVersion();
      if (j != -1)
        this.mimeMinorVersion = j;
      this.isMimeVersionSet = true;
      return;
    }
    catch (MimeException localMimeException)
    {
      this.mimeVersionException = localMimeException;
    }
  }

  public void addField(String paramString1, String paramString2)
  {
    String str = paramString1.trim().toLowerCase();
    if (("mime-version".equals(str)) && (!(this.isMimeVersionSet)))
      parseMimeVersion(paramString2);
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  return;
                  if ((!("content-id".equals(str))) || (this.isContentIdSet))
                    break;
                  parseContentId(paramString2);
                }
                if ((!("content-description".equals(str))) || (this.isContentDescriptionSet))
                  break;
                parseContentDescription(paramString2);
              }
              if ((!("content-disposition".equals(str))) || (this.isContentDispositionSet))
                break;
              parseContentDisposition(paramString2);
            }
            if ((!("content-language".equals(str))) || (this.isContentLanguageSet))
              break;
            parseLanguage(paramString2);
          }
          if ((!("content-location".equals(str))) || (this.isContentLocationSet))
            break;
          parseLocation(paramString2);
        }
        if ((!("content-md5".equals(str))) || (this.isContentMD5Set))
          break;
        parseMD5(paramString2);
      }
      super.addField(str, paramString2);
    }
  }

  public String getContentDescription()
  {
    return this.contentDescription;
  }

  public DateTime getContentDispositionCreationDate()
  {
    return this.contentDispositionCreationDate;
  }

  public MimeException getContentDispositionCreationDateParseException()
  {
    return this.contentDispositionCreationDateParseException;
  }

  public String getContentDispositionFilename()
  {
    return ((String)this.contentDispositionParameters.get("filename"));
  }

  public DateTime getContentDispositionModificationDate()
  {
    return this.contentDispositionModificationDate;
  }

  public MimeException getContentDispositionModificationDateParseException()
  {
    return this.contentDispositionModificationDateParseException;
  }

  public Map getContentDispositionParameters()
  {
    return this.contentDispositionParameters;
  }

  public DateTime getContentDispositionReadDate()
  {
    return this.contentDispositionReadDate;
  }

  public MimeException getContentDispositionReadDateParseException()
  {
    return this.contentDispositionReadDateParseException;
  }

  public long getContentDispositionSize()
  {
    return this.contentDispositionSize;
  }

  public MimeException getContentDispositionSizeParseException()
  {
    return this.contentDispositionSizeParseException;
  }

  public String getContentDispositionType()
  {
    return this.contentDispositionType;
  }

  public String getContentId()
  {
    return this.contentId;
  }

  public List getContentLanguage()
  {
    return this.contentLanguage;
  }

  public MimeException getContentLanguageParseException()
  {
    return this.contentLanguageParseException;
  }

  public String getContentLocation()
  {
    return this.contentLocation;
  }

  public MimeException getContentLocationParseException()
  {
    return this.contentLocationParseException;
  }

  public String getContentMD5Raw()
  {
    return this.contentMD5Raw;
  }

  public int getMimeMajorVersion()
  {
    return this.mimeMajorVersion;
  }

  public int getMimeMinorVersion()
  {
    return this.mimeMinorVersion;
  }

  public MimeException getMimeVersionParseException()
  {
    return this.mimeVersionException;
  }
}